import { Blog } from "../model/Blog.js"
import { Op } from "sequelize";

let fn_list=async (ctx,next)=>{

    let keyword = ctx.request.query.keyword || ' ';
    let where= null
    if (keyword.trim()) {
        where={
            [Op.or]:{
                title:{
                    [Op.like]:`%${keyword}%`
                },
                author:{
                    [Op.like]:`%${keyword}%`
                }
            }
        }
    }


    let blogs =await Blog.findAll({where})

    ctx.body={
        code:1000,
        msg:'博客列表',
        data:blogs
    }
}
let fn_getById=async (ctx,next)=>{
    let id = ctx.params.id || 0
    let blogs= await Blog.findByPk(id)
    ctx.body={
        code:1000,
        msg:'请求成功',
        data:blogs
    }
}
let fn_add=async (ctx,next)=>{
    let obj= ctx.request.body
    let blogs = await Blog.create(obj)

    ctx.body={
        code:1000,
        msg:'新增成功',
        data:blogs
    }
}
let fn_edit=async (ctx,next)=>{
    ctx.body='编辑博客'
}
let fn_del=async (ctx,next)=>{
    let id =
    ctx.body='删除博客'
}

export const obj = {
    "get /blogs":fn_list,
    "get /blogs/:id":fn_getById,
    "post /blogs":fn_add,
    "put /blogs/:id":fn_edit,
    "delete /blogs/:id":fn_del
}

